<!DOCTYPE HTML>
<html class="bg-eee">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/layer.css" />
    <link rel="stylesheet" type="text/css" href="../css/mdw.css" />
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../script/swiper.min.js"></script>
    <script type="text/javascript" src="../script/mobile.js"></script>
    <script type="text/javascript" src="../script/layer.js"></script>
    <script type="text/javascript" src="../script/mdw.js"></script>
    <script type="text/javascript" src="../script/jumpPage.js"></script>

</head>
<body class="bg-eee">
    <header class="header">
        <div class="backPrevPage" onclick="historyBack()"></div>
        <div class="pageTitle">购物车</div>
        <div class="headerRight c-333" >编辑</div>
    </header>
    <section class="shoppingPage HTML_BOX">
        <div class="bg-eee" style="border-bottom:.2rem solid #EEE ; "></div>
    </section>
    <section class="footer shoppingFooter displayFlex submitFoot">
        <div class="footLeft"> <i class="allRadio radio"></i><span class="allselect">全选</span>
            <span class="spanBox1">合计:</span><span style="margin-top: 0.05rem;margin-left: 0.1rem;" class="c_main">$0.00</span></div>
        <div class="footRight ">
            <div id="addOrder" class="footbtn fb1 f_c submit">结算（<span class="shoppingCount">0</span>）</div>
        </div>
    </section>
    <section class="footer shoppingFooter displayFlex updateFoot" style="display: none">
        <div class="footLeft"> <i class="allRadio radio"></i><span class="allselect">全选</span></div>
        <div class="footRight ">
            <div id="scj" class="footbtn fb2 f_c submit">移入收藏夹</div>
            <div class="footbtn fb1 f_c delete">删除</div>
        </div>
    </section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    $(".headerRight").click(function(){
        var state = $(this).html();
        if(cnen==1){
          if(state=="编辑"){
              $(this).html("完成");
              $(".updateFoot").show();
              $(".submitFoot").hide();
          }else{
              $(this).html("编辑");
              $(".updateFoot").hide();
              $(".submitFoot").show();
          }
        }else{
          if(state=="Edit"){
              $(this).html("Finish");
              $(".updateFoot").show();
              $(".submitFoot").hide();
          }else{
              $(this).html("Edit");
              $(".updateFoot").hide();
              $(".submitFoot").show();
          }
        }
    })
    //选择商品
    function wareRadio(e){
          var has = $(e).hasClass("on");
          var hid = $(e).data("sid");
          if(has){
              $(e).removeClass("on");

          }else{
              $(e).addClass("on");
          }
          ifAllChoose(hid);
          shoppingCount();
          ifallradio();
    }
    //选择商铺
    function shopIcon(e){
        var has = $(e).hasClass("on");
        var id = $(e).data("id");
        if(has){
            $(e).removeClass("on");
            $(".radio_"+id).removeClass("on");
        }else{
            $(e).addClass("on");
            $(".radio_"+id).addClass("on");
        }
        
        shoppingCount();
        ifallradio();
    };
    //全选
    $(".allRadio").click(function(){
        var has = $(".allRadio").hasClass("on");
        if(has){
            $(".allRadio").removeClass("on");
            $(".radio").removeClass("on");
        }else{
            $(".allRadio").addClass("on");
            $(".radio").addClass("on");
        }
        shoppingCount();
    })
    //删除
    $(".delete").click(function(){
        var count  = $(".shoppingCount").html();
        if(Number(count) == 0){
            pop.notice("请选择一个商品");
            return;
        }
        var ids = "";
        var index = 0;
        var type = 1;
        $(".wareRadio.on").each(function(i,item){
            var id = $(item).data("id");
            if(index == 0){
              ids +=""+id;
            }else{
              type=2;
              ids +=","+id;
            }
            index++;
        });
        pop.loading();
        var p = {token:token,type:type,id:ids};
        console.log(JSON.stringify(p));
        getAjax(Interface.car_del,p,function(res){
          console.log(JSON.stringify(res));
          pop.close();
          pop.notice('删除成功');
          var ifall = true;
          $(".radio.on").each(function(i,item){
              var has1 = $(item).hasClass("shopIcon");
              var has2 = $(item).hasClass("wareRadio");
              if(has1){
                  var id = $(item).data("id");
                  console.log(id);
                  $("#shopItem_"+id).remove();
              }
              if(has2){
                  $(item).parent().remove();
              }
          });
        },function(res,err){})
        $(".allRadio").removeClass("on");
    })
    //移入收藏夹
    $("#scj").click(function(){
        var count  = $(".shoppingCount").html();
        if(Number(count) == 0){
            pop.notice("请选择一个商品");
            return;
        }
        var ids = "";
        var index = 0;
        var type = 1;
        $(".wareRadio.on").each(function(i,item){
            var id = $(item).data("pid");
            if(index == 0){
              ids +=id;
            }else{
              type=2;
              ids +=","+id;
            }
            index++;
        });
        pop.loading();
        var p = {token:token,type:1,product_id:ids};
        console.log(JSON.stringify(p));
        getAjax(Interface.join_collect,p,function(res){
          console.log(JSON.stringify(res));
          pop.close();
          pop.notice(res.msg);
        },function(res,err){})
        $(".allRadio").removeClass("on");
    })
    //计算选择个数，价格
    function shoppingCount(){
        var count = 0;
        var price = 0;
        $(".wareRadio.on").each(function(i,item){
            var pr = $(item).data("price");
            var id = $(item).data("id");
            var val = $("#ware_"+id).val();
            price +=Number(pr) * Number(val);
            count++;
        });
        $(".c_main").html("$"+price.toFixed(2));
        $(".shoppingCount").html(count);
    }
    // function shopScore(id){
    //     if(id != 'undefined'){
    //       var score = 0;
    //       $(".radio_"+id).each(function(i,item){
    //         var has = $(item).hasClass("on");
    //         if(has){
    //           var pr = $(item).data("price");
    //           var id = $(item).data("id");
    //           var val = $("#ware_"+id).val();
    //           price +=Number(pr) * Number(val);
    //           score +=Number($(item).data("price"));
    //         }
    //       });
    //       $("#shopCount_"+id).html(parseInt(score));
    //     }else{

    //     }
       
        
    // }


    //判断是否全选商铺
    function ifAllChoose(id){
        var ifall = true;
        $(".radio_"+id).each(function(i,item){
            var has = $(item).hasClass("on");
            if(!has){
                ifall = false;
            }
        });
        if(ifall){
            $("#shop_"+id).addClass("on");
        }else{
            $("#shop_"+id).removeClass("on");
        }
    }
    //判断是否全选
    function ifallradio(){
        var ifall = true;
        $(".wareRadio").each(function(i,item){
            var has = $(item).hasClass("on");
            if(!has){
                ifall = false;
            }
        });
        if(ifall){
            $(".allRadio").addClass("on");
        }else{
            $(".allRadio").removeClass("on");
        }

    }
    var iscount = false;
    //数量加减
    $("body").on("click", ".count_control .reduce",function(){
      var val = $(this).next().find("input").val();
         console.log(val);
        if(val ==1){
            if(cnen==1){
                pop.notice('数量不能小于1');
            }else{
                pop.notice('Count should not be < 1');
            }
            return;
        }
        if(iscount){
          return;
        }
        var id =$(this).data("id");
        var p = {token:token,car_id:id,type:1};
        //alert(JSON.stringify(p));
        var car = this;
        getAjax(Interface.change_car_num,p,function(res){
          iscount = false;
          val= Number(val)-Number(1);
          $(car).next().find("input").val(val);
          shoppingCount();
        },function(res,err){iscount = false;});
        
    });
    $("body").on("click", ".count_control .add",function(){
        if(iscount){
          return;
        }
        iscount = true;
        var id =$(this).data("id");
        var p = {token:token,car_id:id,type:2};
        //alert(JSON.stringify(p));
        var cars = $(this);
        getAjax(Interface.change_car_num,p,function(res){
          iscount = false;
          var val =cars.prev().find("input").val();
          console.log(val);
          val= Number(val)+Number(1);
          console.log(val);
          cars.prev().find("input").val(val);
          shoppingCount();
        },function(res,err){
          iscount = false;

        });
    });
    // $("body").on("change", ".count_control input",function(){
    //     var val =$(this).val();
    //     if(val < 1){
    //         if(cnen==1){
    //             pop.notice('数量不能小于1');
    //         }else{
    //             pop.notice('Count should not be < 1');
    //         }
    //         $(this).val(1);
    //     }
    //     shoppingCount();
    // });
    apiready = function(){
        $api.fixStatusBar($api.dom('header'));
        api.setStatusBarStyle({
            color: '#fff',//设置APP状态栏背景色
            style: 'dark'
        });
        ifCnAndEn();
        ready();
    };
    $("#addOrder").click(function(){
        var count  = $(".shoppingCount").html();
        if(Number(count) == 0){
            pop.notice("请选择一个商品");
            return;
        }
        var ids = "";
        var index = 0;
        $(".wareRadio.on").each(function(i,item){
            var id = $(item).data("id");
            if(index == 0){
              ids +=id;
            }else{
              ids +=","+id;
            }
            index++;
        });
        var p = {ids:ids};
        openWin('ware_order','ware_order.html',p);
    })
    function ready(){
        getAjax(Interface.car_info,{token:token},function(res){
            var list = res.data.car_list;//购物车列表
            console.log(JSON.stringify(res));
            if(list !=""&& list !=null && list !=false && list.length !=0){
                var html = '';
                $.each(list,function(i,item){
                  var shop_name = item.username;
                  if(cnen ==2){
                    shop_name = item.egusername;
                  }
                  if(item.son_list.length > 0){
                      html +='<div id="shopItem_'+i+'" class="shopItem bg-fff">';
                      html +=' <div class="shopInfo plr"><i id="shop_'+i+'" onclick="shopIcon(this)" class="shopIcon radio" data-id="'+i+'"></i>'+shop_name+'</div>';
                      html +=' <ul class="shoppingWareList plr">';
                      $.each(item.son_list,function(j,jtem){
                          html+='<li><i data-sid="'+i+'" data-id='+jtem.car_id+' data-price="'+jtem.new_price+'" onclick="wareRadio(this)" data-pid='+jtem.id+' class="radio_'+i+' wareRadio radio"></i>';
                          html+='<div class="shoppingInfo"><div onclick="goWare(\''+jtem.id+'\')" class="wareImage"><img src="'+getImg(jtem.icon)+'" /></div>';
                          var name = jtem.title;
                          if(cnen ==2){
                              name = jtem.egname;
                          }
                          html+='<div class="wareData"><div class="wareName"><span>'+name+'</span></div>';
                          html+='<div class="wareGuiGe">';
                          if(jtem.shuilv_id>0){
                           html += '<i></i>';
                          }
                          html +='<div class="pstit f_c '+getSend(jtem.sendtype_name)+'">'+jtem.sendtype_name+'</div></div>';
                          html+='<div class="warePrice"><div>';
                          if(jtem.old_price == ""){
                              html+='<span class="prcie1">$'+jtem.new_price+'</span>';
                          }else{
                              html+='<span class="prcie1">$'+jtem.new_price+'</span>';
                              html+='<del class="prcie2">'+jtem.old_price+'</del>';
                          }
                          html+='</div></div></div><div class="count_control">';
                          html+=' <div data-id="'+jtem.car_id+'" class="btn reduce">-</div>';
                          html+='<div class="input"><input readonly="readonly" id="ware_'+jtem.car_id+'" value="'+jtem.num+'" type="number" /></div>';
                          html+='<div data-id="'+jtem.car_id+'" class="btn add">+</div>';
                          html+='</div> </div></li>';
                      })
                       html +='</ul>';
                      //html +='<div class="shoppingYh plr ">下单获得<span id="shopCount_'+i+'" >0</span>积分</div>';
                      //html +='<div class="shoppingYh plr ">下单获得<span></span>积分  已优惠：￥40   </div>';
                      html +='</div>';
                  }
                })
                $(".shoppingPage").append(html);
            }
        },function(res,err){})
    }

    function getSend(title){
        if(title=="当日送达" || title=="Same-day Delivery"){
            return"t1";
        }else if(title=="三日送达" || title=="3-day Delivery"){
            return"t2";
        }else if(title=="商家派送" || title=="Merchant Delivery"){
            return"t3";
        }else if(title=="海外集运" || title=="Overseas Delivery"){
            return"t4";
        }else if(title=="预购待送" || title=="Pre-order"){
            return"t5";
        }else if(title=="电子优惠券" || title=="E-Coupon"){
            return"t6";
        }else{
            return"t1";
        }
    }
</script>
<script type="text/javascript">
    function ifCnAndEn(){
        if(cnen==2){
           $(".pageTitle").html("Your cart");
           $(".headerRight").html("Edit");
           $(".allselect").html('All');
           $(".spanBox1").html('Total:');
           $("#addOrder").html('Checkout (<span class="shoppingCount">0</span>)');
           $(".fb2").html('Add Wish List');
           $(".delete").html('Delete');
        }
    }
</script>

</html>
